;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Stock.PickingBill", [])
        .controller("PickingBillCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "EventBus", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, EventBus, UserContextService) {
            $scope.activeFlag = [true, false, false, false, false, false, false];
            $scope.datePickerFlag = [false, false];
            $scope.markInfo = {
                allCheckedBill: false,
                allCheckedMaterial: false,
                curRow: -1
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.pageInfoDel = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };            
            $scope.submitted = false;
            $scope.condition = {};
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false,
                showMateriel: false
            };
            $scope.statusList = [{
                    id: 0,
                    name: "未开始"
                },
                {
                    id: 1,
                    name: "进行中"
                },
                {
                    id: 2,
                    name: "已完成"
                },
                {
                    id: 3,
                    name: "强制结单"
                }
            ];
            $scope.dataList = [];
            // 已删除的
            $scope.dataDelList = [];
            $scope.tbConf = {
                fixedWidth: 450,
                scrollWidth: 1700,
                tableCol: [
                    {
                        type: "",
                        name: "领料单号",
                        dataField: "requisitionNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                    },
                    {
                        type: "",
                        name: "领入仓库",
                        dataField: "inLocation",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领入过账状态",
                        dataField: "postInStatusShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领入零件凭证",
                        width: 120,
                        dataField: "voucherInNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领出过账状态",
                        dataField: "postOutStatusShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领出零件凭证",
                        width: 120,
                        dataField: "voucherOutNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "单据类型",
                        dataField: "documentName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                    },
                    {
                        type: "",
                        name: "制单部门",
                        dataField: "createPartName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "制单人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "出入库状态",
                        dataField: "statusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "回执状态",
                        dataField: "receiptStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "synSap",
                        name: "SAP同步状态",
                        dataField: "sapStatusShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewSapError",
                    },
                    {
                        type: "",
                        name: "SAP领出预留号",
                        dataField: "sapOutCode",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "SAP领入预留号",
                        dataField: "sapInCode",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "备注",
                        dataField: "note",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核状态",
                        dataField: "auditStatusShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核人",
                        dataField: "auditUserName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核时间",
                        dataField: "auditDateShow",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核意见",
                        dataField: "auditOpinion",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "CRM单号",
                        dataField: "crmOrder",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "打印状态",
                        dataField: "isPrintName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "打印时间",
                        dataField: "printTimeShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "需求时间",
                        dataField: "requireTimeShow",
                        width: 150,
                        isShow: !!!$scope.isSCH,
                        isFixed: false,
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 380,
                        isShow: true,
                        isFixed: true,
                    }
                ]
            };  
            $scope.tbConfDel = {
                fixedWidth: 0,
                scrollWidth: 1700,
                tableCol: [
                    {
                        type: "",
                        name: "领料单号",
                        dataField: "requisitionNo",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewDetail",
                    },
                    {
                        type: "",
                        name: "领入仓库",
                        dataField: "inLocation",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领入过账状态",
                        dataField: "postInStatusShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领入零件凭证",
                        width: 120,
                        dataField: "voucherInNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领出过账状态",
                        dataField: "postOutStatusShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "领出零件凭证",
                        width: 120,
                        dataField: "voucherOutNo",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "单据类型",
                        dataField: "documentName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                    },
                    {
                        type: "",
                        name: "制单部门",
                        dataField: "createPartName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "制单人",
                        dataField: "createName",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "出入库状态",
                        dataField: "statusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "回执状态",
                        dataField: "receiptStatusShow",
                        width: 100,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "synSap",
                        name: "SAP同步状态",
                        dataField: "sapStatusShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        clickFn: "viewSapError",
                    },
                    {
                        type: "",
                        name: "SAP领出预留号",
                        dataField: "sapOutCode",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "SAP领入预留号",
                        dataField: "sapInCode",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "备注",
                        dataField: "note",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核状态",
                        dataField: "auditStatusShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核人",
                        dataField: "auditUserName",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核时间",
                        dataField: "auditDateShow",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        type: "",
                        name: "审核意见",
                        dataField: "auditOpinion",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "CRM单号",
                        dataField: "crmOrder",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "打印状态",
                        dataField: "isPrintName",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "打印时间",
                        dataField: "printTimeShow",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                    }, 
                ]
            };                        
            $scope.inTbConf = {
                scrollWidth: 890,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "分类",
                        width: 150,
                        dataField: "materialsType",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "单位",
                        width: 120,
                        dataField: "unit",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出仓库",
                        width: 150,
                        dataField: "outLocation",
                        input: true,
                        inputType: "txt",
                        isShow: true
                    },
                    {
                        name: "领料数量",
                        width: 150,
                        dataField: "amount",
                        input: true,
                        inputType: "num",
                        isShow: true
                    },
                    {
                        name: "大厂库存",
                        width: 150,
                        dataField: "totalStock",
                        input: false,
                        inputType: "",
                        clickFn: 'dachangStock',
                        isShow: true
                    },
                ]
            };
            $scope.editInTbConf = {
                scrollWidth: 1310,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "分类",
                        width: 100,
                        dataField: "materialsType",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出仓库",
                        width: 120,
                        dataField: "outLocation",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入仓库",
                        width: 120,
                        dataField: "inLocation",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入库区",
                        width: 120,
                        dataField: "inArea",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领料数量",
                        width: 120,
                        dataField: "amount",
                        input: true,
                        inputType: "num",
                        isShow: true
                    },
                    {
                        name: "实际入库数量",
                        width: 120,
                        dataField: "realAmount",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入零件凭证",
                        width: 120,
                        dataField: "voucherInNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入过账状态",
                        width: 120,
                        dataField: "postInStatusShow",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出零件凭证",
                        width: 120,
                        dataField: "voucherOutNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出过账状态",
                        width: 120,
                        dataField: "postOutStatusShow",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "入库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                ]
            };
            $scope.viewInTbConf = {
                scrollWidth: 1310,
                tableCol: [
                    {
                        type: "",
                        name: "序号",
                        dataField: "rownum",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "分类",
                        width: 100,
                        dataField: "materialsType",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领出仓库",
                        width: 120,
                        dataField: "outLocation",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领入仓库",
                        width: 120,
                        dataField: "inLocation",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领入库区",
                        width: 120,
                        dataField: "inArea",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领料数量",
                        width: 120,
                        dataField: "amount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "实际入库数量",
                        width: 120,
                        dataField: "realAmount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "已过账数量",
                        width: 120,
                        dataField: "postAmount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领入零件凭证",
                        width: 120,
                        dataField: "voucherInNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领入过账状态",
                        width: 120,
                        dataField: "postInStatusShow",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领出零件凭证",
                        width: 120,
                        dataField: "voucherOutNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领出过账状态",
                        width: 120,
                        dataField: "postOutStatusShow",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "入库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "发货通知单号",
                        width: 160,
                        dataField: "deliveryNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "钢印号",
                        width: 160,
                        dataField: "steelNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "实际入库时间",
                        width: 160,
                        dataField: "inTimeShow",
                        input: false,
                        inputType: ""
                    },
                ]
            };
            $scope.adjustInTbConf = {
                scrollWidth: 1310,
                tableCol: [
                    {
                        type: "",
                        name: "序号",
                        dataField: "rownum",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                        isShow: true
                    },
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "分类",
                        width: 100,
                        dataField: "materialsType",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出仓库",
                        width: 120,
                        dataField: "outLocation",
                        input: true,
                        inputType: "txt",
                        isShow: true
                    },
                    {
                        name: "领入仓库",
                        width: 120,
                        dataField: "inLocation",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入库区",
                        width: 120,
                        dataField: "inArea",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领料数量",
                        width: 120,
                        dataField: "amount",
                        input: true,
                        inputType: "num",
                        isShow: true
                    },
                    {
                        name: "实际入库数量",
                        width: 120,
                        dataField: "realAmount",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "已过账数量",
                        width: 120,
                        dataField: "postAmount",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "零件凭证",
                        width: 120,
                        dataField: "voucherNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "入库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                ]
            };
            $scope.outTbConf = {
                scrollWidth: 980,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "分类",
                        width: 100,
                        dataField: "materialsType",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领料数量",
                        width: 120,
                        dataField: "amount",
                        input: true,
                        inputType: "num",
                        isShow: true
                    },
                    {
                        name: "实际库存",
                        width: 100,
                        dataField: "enableNum",
                        clickFn: "alterReport",
                        needInput: false,
                        isShow: true
                    },
                    {
                        name: "意向占用",
                        dataField: "intentionNum",
                        width: 100,
                        input: false,
                        clickFn: "intentionDetail",
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "发货占用",
                        dataField: "deliveryNum",
                        width: 100,
                        input: false,
                        clickFn: "deliveryDetail",
                        inputType: "",
                        isShow: true
                    },
                    
                ]
            };
            $scope.editOutTbConf = {
                scrollWidth: 1270,
                tableCol: [
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "分类",
                        width: 100,
                        dataField: "materialsType",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出仓库",
                        width: 120,
                        dataField: "outLocation",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出库区",
                        width: 120,
                        dataField: "outArea",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入仓库",
                        width: 120,
                        dataField: "inLocation",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领料数量",
                        width: 120,
                        dataField: "amount",
                        input: true,
                        inputType: "num",
                        isShow: true
                    },
                    {
                        name: "实际出库数量",
                        width: 120,
                        dataField: "realAmount",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入零件凭证",
                        width: 120,
                        dataField: "voucherInNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领入过账状态",
                        width: 120,
                        dataField: "postInStatusShow",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出零件凭证",
                        width: 120,
                        dataField: "voucherOutNo",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "领出过账状态",
                        width: 120,
                        dataField: "postOutStatusShow",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                    {
                        name: "出库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: "",
                        isShow: true
                    },
                ]
            };
            $scope.viewOutTbConf = {
                scrollWidth: 1210,
                tableCol: [
                    {
                        type: "",
                        name: "序号",
                        dataField: "rownum",
                        width: 80,
                        isShow: true,
                        isFixed: false,
                    },
                    {
                        name: "零件编号",
                        width: 150,
                        dataField: "materialsNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "零件中文名",
                        width: 200,
                        dataField: "materialsDes",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "分类",
                        width: 100,
                        dataField: "materialsType",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "单位",
                        width: 100,
                        dataField: "unit",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领出仓库",
                        width: 120,
                        dataField: "outLocation",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领出库区",
                        width: 120,
                        dataField: "outArea",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领入仓库",
                        width: 120,
                        dataField: "inLocation",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领料数量",
                        width: 120,
                        dataField: "amount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "实际出库数量",
                        width: 120,
                        dataField: "realAmount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "已过账数量",
                        width: 120,
                        dataField: "postAmount",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领入零件凭证",
                        width: 120,
                        dataField: "voucherInNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领入过账状态",
                        width: 120,
                        dataField: "postInStatusShow",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领出零件凭证",
                        width: 120,
                        dataField: "voucherOutNo",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "领出过账状态",
                        width: 120,
                        dataField: "postOutStatusShow",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "出库状态",
                        width: 100,
                        dataField: "statusShow",
                        input: false,
                        inputType: ""
                    },
                    {
                        name: "实际出库时间",
                        width: 160,
                        dataField: "outTimeShow",
                        input: false,
                        inputType: ""
                    }
                ]
            };
            $scope.billDetail = {
                headerInfo: {},
                detailList: []
            };
            $scope.exportParam = {
                sectionUrl: ybHttp.RequestUrl("device", "/materialsSchedule/export"),
                sectionParam: {
                    ids: []
                },
                allUrl: ybHttp.RequestUrl("device", "/materialsSchedule/exportAll")
            };
            $scope.tabFlag = {};

            var btnPermissionCode = {
                pickingBillInAdd: window.ActionCode.pickingBillInAdd,
                pickingBillOutAdd: window.ActionCode.pickingBillOutAdd,
                pickingBillEdit: window.ActionCode.pickingBillEdit,
                pickingBillAudit: window.ActionCode.pickingBillAudit,
                pickingBillDel: window.ActionCode.pickingBillDel,
                pickingBillPrint: window.ActionCode.pickingBillPrint,
                pickingBillExport: window.ActionCode.pickingBillExport,
                pickingBillHandle: window.ActionCode.pickingBillHandle,
                pickingBillObligateAdd: window.ActionCode.pickingBillObligateAdd,
                pickingBillObligatePosting: window.ActionCode.pickingBillObligatePosting,
                mandatoryFinish: window.ActionCode.pickingBillMandatoryFinish,
                pickingBillReceipt: window.ActionCode.pickingBillReceipt,
                pickingBillInBound: window.ActionCode.pickingBillInBound,
                pickingBillOutBound: window.ActionCode.pickingBillOutBound
            };

            $scope.btnPermission = {
                pickingBillInAdd: true,
                pickingBillOutAdd: true,
                pickingBillEdit: true,
                pickingBillAudit: true,
                pickingBillDel: true,
                pickingBillPrint: true,
                pickingBillExport: true,
                pickingBillHandle: true,
                pickingBillObligateAdd: true,
                pickingBillObligatePosting: true,
                mandatoryFinish: true,
                pickingBillReceipt: true,
                pickingBillInBound: true,
                pickingBillOutBound:true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.setFixedWidth = function () {
                $scope.tbStyle = {
                    fixedWidth: {
                        left: $scope.tbConf.fixedWidth + "px"
                    }
                };
            };

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false, false, false, false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    case 3:
                        break;
                    case 4:
                        break;
                    case 5:
                        break;
                    case 6:
                        break;
                    case 7:
                        break;
                    default:
                        break;
                }
            };

            function initData() {
                $scope.billDetail = {
                    headerInfo: {},
                    detailList: []
                };
                $scope.tabFlag = {};
                $scope.markInfo.allCheckedMaterial = false;
                $scope.toolbarConfig.showMateriel = false;
            }

            function getDataList(curPage) {
                $scope.markInfo = {
                    allCheckedBill: false,
                    allCheckedMaterial: false,
                    curRow: -1
                };
                $scope.exportParam.sectionParam.ids = [];
                var service = "device";
                var reqUrl = "/materialsSchedule/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                param.filters.isProcessing = null;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        
                        item.sapStatusShow = $filter("ProgressStatus")(item.sapStatus, "sap");
                        item.receiptStatusShow = $filter("ProgressStatus")(item.isReceipt, "receiptState");
                        item.auditStatusShow = $filter("AuditStatus2")(item.auditStatus);
                        item.auditDateShow = $filter("YbDate")(item.auditDate, "yyyy-MM-dd HH:mm:ss");
                        item.printTimeShow = $filter("YbDate")(item.printTime, "yyyy-MM-dd HH:mm");
                        item.requireTimeShow = $filter("YbDate")(item.requireTime, "yyyy-MM-dd HH:mm:ss");

                        if (item.type === 0) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                            item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                        }
                        if (item.type === 1) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                            if (!$scope.isSCH) {
                                item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting"); 
                            }
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                        }
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.selectRow = function (index) {
                $scope.markInfo.curRow = index
            };

            $scope.tdClick = function (item, fnType, type) {
                switch (fnType) {
                    case "viewDetail":
                        viewDetail(item, type);
                        break;
                    case "viewSapError":
                        viewSapError(item);
                        break;
                    case "alterReport":
                        alterReport(item);
                        break;
                    case "intentionDetail":
                        intentionDetail(item);
                        break;
                    case "deliveryDetail":
                        deliveryDetail(item);
                        break;
                    case "dachangStock":
                        dachangStock(item);
                        break;
                    default:
                        break;
                }
            };

            function dachangStock(item) {
                var param = {
                    title: '大厂库存',
                    data: item.detailVOList || []
                };
                ybWidget.DatasShow(param);
            }

            function alterReport(item) {
                var param = {
                    materialsNo: item.materialsNo
                };
                ybWidget.StorageReport(param);
            }

            function intentionDetail(item) {
                var param = {
                    materialsNo: item.materialsNo
                };
                ybWidget.IntentionDetail(param);
            }

            function deliveryDetail(item) {
                var param = {
                    materialsNo: item.materialsNo,
                    warehouseCode: $scope.billDetail.headerInfo.outLocation
                };
                ybWidget.DeliveryDetail(param);
            }

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false, false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };

            function getBillIds() {
                $scope.exportParam.sectionParam.ids = [];
                angular.forEach($scope.dataList, function (item) {
                    if (!!item.isChecked) {
                        $scope.exportParam.sectionParam.ids.push(item.id);
                    }
                });
            }

            $scope.selectSingle = function (item, list, type) {
                if (!item.isChecked) {
                    $scope.markInfo["allChecked" + type] = false;
                } else {
                    if (!checkAll(list)) {
                        $scope.markInfo["allChecked" + type] = false;
                    } else {
                        $scope.markInfo["allChecked" + type] = true;
                    }
                }
                getBillIds();
            };

            function checkAll(list) {
                var selectAll = false;
                for (var i = 0, len = list.length; i < len; i++) {
                    if (!list[i].isChecked) {
                        selectAll = false;
                        break;
                    }
                    selectAll = true;
                }
                return selectAll;
            }

            $scope.selectAll = function (list, type) {
                angular.forEach(list, function (item) {
                    item.isChecked = $scope.markInfo["allChecked" + type];
                });
                getBillIds();
            };
            function ChyTable() {
                if(!$scope.isSCH) return
                let arr = [
                    {
                        name: "需求时间",
                        width: 150,
                        dataField: "requireTime",
                        input: true,
                        inputType: "time",
                        isShow: true                 
                   },
                    {
                        name: "备注",
                        width: 150,
                        dataField: "remark",
                        input: true,
                        inputType: "input",
                        isShow: true
                    }
                ]
                let arrShow = [
                    {
                        name: "需求时间",
                        width: 150,
                        dataField: "requireTimeShow",
                        input: true,
                        inputType: "time",
                        isShow: true                 
                   },
                    {
                        name: "备注",
                        width: 150,
                        dataField: "remark",
                        input: true,
                        inputType: "input",
                        isShow: true
                    },
                    {
                        name: "领入过账时间",
                        width: 150,
                        dataField: "postInDateShow",
                        isShow: true                 
                   },
                    {
                        name: "领出过账时间",
                        width: 150,
                        dataField: "postOutDateShow",
                        isShow: true                 
                   },
                ]
                $scope.inTbConf.tableCol.push(...arr)
                $scope.editInTbConf.tableCol.push(...arr)
                $scope.adjustInTbConf.tableCol.push(...arr)
                $scope.outTbConf.tableCol.push(...arr)
                $scope.editOutTbConf.tableCol.push(...arr)
                $scope.viewOutTbConf.tableCol.push(...arrShow)
                $scope.viewInTbConf.tableCol.push(...arrShow)
            }
            $scope.pickingIn = function () {
                $scope.tabFlag.type = 0;
                $scope.billDetail.headerInfo.type = 0;
                $scope.billDetail.headerInfo.billTypeEcs = "从生产领料";
                $scope.billDetail.headerInfo.createDate = +new Date();
                $scope.billDetail.headerInfo.createPartId = $scope.curUser.deptId;
                $scope.billDetail.headerInfo.createPartName = $scope.curUser.deptName;
                $scope.billDetail.headerInfo.createId = $scope.curUser.userId;
                $scope.billDetail.headerInfo.createName = $scope.curUser.name;

                if ($scope.isWJ) {
                    $scope.billDetail.headerInfo.inLocation = 'WJBJ'
                    $scope.billDetail.headerInfo.inArea = '9000'
                    $scope.billDetail.headerInfo.sapInArea = '9000'
                    $scope.billDetail.headerInfo.sapType = '311'
                }

                $scope.changeTab(1);
            };
            $scope.delHistory = function (curPage) {
                $scope.tabFlag.type = 11;
                $scope.billDetail.headerInfo.type = 11;
                $scope.billDetail.headerInfo.billTypeEcs = "已删除记录";
                $scope.changeTab(11);
                var service = "device";
                var reqUrl = "/materialsSchedule/list"
                var param = {}
                param.pageSize = $scope.pageInfoDel.pageSize;
                if (!!curPage) {
                    $scope.pageInfoDel.curPage = curPage;
                }
                param.pageNum = $scope.pageInfoDel.curPage;
                param.filters = angular.copy($scope.condition);
                param.filters.isProcessing = null;
                param.filters.isDel = 1;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        if (item.type === 0) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                            item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                        }
                        if (item.type === 1) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                            if (!$scope.isSCH) {
                                item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                            }
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                        }
                        item.sapStatusShow = $filter("ProgressStatus")(item.sapStatus, "sap");
                        item.receiptStatusShow = $filter("ProgressStatus")(item.isReceipt, "receiptState");
                        item.auditStatusShow = $filter("AuditStatus2")(item.auditStatus);
                        item.auditDateShow = $filter("YbDate")(item.auditDate, "yyyy-MM-dd HH:mm:ss");
                        item.printTimeShow = $filter("YbDate")(item.printTime, "yyyy-MM-dd HH:mm");
                    });
                    $scope.pageInfoDel.totalRows = resp.value.total;
                    $scope.pageInfoDel.totalPages = resp.value.pages;
                    $scope.dataDelList = resp.value.list;
                });                
            };
            $scope.pickingOut = function () {
                $scope.tabFlag.type = 1;
                $scope.billDetail.headerInfo.type = 1;
                $scope.billDetail.headerInfo.billTypeEcs = "从备件领料";
                $scope.billDetail.headerInfo.createDate = +new Date();
                $scope.billDetail.headerInfo.createPartId = $scope.curUser.deptId;
                $scope.billDetail.headerInfo.createPartName = $scope.curUser.deptName;
                $scope.billDetail.headerInfo.createId = $scope.curUser.userId;
                $scope.billDetail.headerInfo.createName = $scope.curUser.name;

                if ($scope.isWJ) {
                    $scope.billDetail.headerInfo.outLocation = 'WJBJ'
                    $scope.billDetail.headerInfo.outArea = '9000'
                    $scope.billDetail.headerInfo.sapOutArea = '9000'
                    $scope.billDetail.headerInfo.sapType = '311'
                }

                $scope.changeTab(4);
            };

            $scope.delData = function (delType, item) {
                var billId = item.id;
                var service = "device";
                var reqUrl = "/materialsSchedule/remove";
                var billIds = [];

                if (delType === "single") {
                    billIds.push(billId);
                }

                if (delType === "batch") {
                    for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                        if (!!$scope.dataList[i].isChecked) {
                            billIds.push($scope.dataList[i].id);
                        }
                    }
                }

                if (billIds.length === 0) {
                    ybWidget.ShowMessage('请选择需要删除的行');
                    return;
                }

                var msg = "确定要删除吗？";
                if (item.sapInCode || item.sapOutCode) {
                    msg = "此领料单已生成预留号，删除请先删除SAP预留，确定要删除吗？";
                }
                ybWidget.ShowMessage(msg, MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, billIds).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            $scope.receipt = function (billId) {
                var service = "device";
                var reqUrl = "/materialsSchedule/receiptConfirm";

                ybWidget.ShowMessage("确定回执吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post(service, reqUrl, {
                        id: billId
                    }).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            $scope.selectWarehouse = function (type) {
                ybWidget.WarehouseSelector().then(function (data) {
                    if (type === "in") {
                        if (!!$scope.billDetail.headerInfo.inLocation && (data.warehouseCode !== $scope.billDetail.headerInfo.inLocation)) {
                            $scope.billDetail.headerInfo.inArea = "";
                            $scope.billDetail.headerInfo.sapInArea = "";
                        }
                        $scope.billDetail.headerInfo.inLocation = data.warehouseCode;
                    }
                    if (type === "out") {
                        if (!!$scope.billDetail.headerInfo.outLocation && (data.warehouseCode !== $scope.billDetail.headerInfo.outLocation)) {
                            $scope.billDetail.headerInfo.outArea = "";
                            $scope.billDetail.headerInfo.sapOutArea = "";
                            clearMateriel();
                        }
                        $scope.billDetail.headerInfo.outLocation = data.warehouseCode;
                    }
                })
            };

            $scope.clearWarehouse = function (type) {
                if (type === "in") {
                    $scope.billDetail.headerInfo.inLocation = "";
                    $scope.billDetail.headerInfo.inArea = "";
                    $scope.billDetail.headerInfo.sapInArea = "";
                }
                if (type === "out") {
                    $scope.billDetail.headerInfo.outLocation = "";
                    $scope.billDetail.headerInfo.outArea = "";
                    $scope.billDetail.headerInfo.sapOutArea = "";
                    clearMateriel();
                }
            };

            $scope.selectArea = function (type) {
                if (type === 'out' && !$scope.billDetail.headerInfo.outLocation) {
                    ybWidget.ShowToaster('error', '请选择领出仓库');
                    return;
                }
                if (type === 'in' && !$scope.billDetail.headerInfo.inLocation) {
                    ybWidget.ShowToaster('error', '请选择领入仓库');
                    return;
                }
                var param = {};
                param.warehouseCode = type === "in" ? $scope.billDetail.headerInfo.inLocation : $scope.billDetail.headerInfo.outLocation;
                ybWidget.DepotAreaSelector(param).then(function (data) {
                    if (type === 'out') {
                        $scope.billDetail.headerInfo.outArea = data.areaCode;
                        $scope.billDetail.headerInfo.sapOutArea = data.sapCode;
                    }
                    if (type === 'in') {
                        $scope.billDetail.headerInfo.inArea = data.areaCode;
                        $scope.billDetail.headerInfo.sapInArea = data.sapCode;
                    }
                })
            };

            $scope.clearArea = function (type) {
                if (type === 'out') {
                    $scope.billDetail.headerInfo.outArea = "";
                    $scope.billDetail.headerInfo.sapOutArea = "";
                    clearMateriel();
                }
                if (type === 'in') {
                    $scope.billDetail.headerInfo.inArea = "";
                    $scope.billDetail.headerInfo.sapInArea = ""
                }
            };

            $scope.selMateriel = function (e) {
                if ($scope.tabFlag.type === 1) {
                    if (!$scope.billDetail.headerInfo.outArea) {
                        ybWidget.ShowToaster('error', '请选择领出库区');
                        return;
                    }
                }
                if (!!$scope.toolbarConfig.showMateriel) {
                    e.stopPropagation();
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig.showMateriel = true;
                        EventBus.Publish("getMaterial");
                    });
                }
            };

            EventBus.Subscribe("onMaterialSelect", pickingBillMaterialChange);

            $scope.sapStockMap = {}

            function pickingBillMaterialChange(item) {
                var service = 'device';
                var reqUrl = '/materialsSchedule/sapStock';
                var param = {
                    materialsNo: item.materialsNo
                };
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.sapStockMap[item.materialsNo] = resp.value || {}
                });
            }

            $scope.importInMateriel = function () {
                var param = {
                    name: "零件",
                    templateReqService: "device",
                    templateReqUrl: "/materialsSchedule/downloadIn",
                    importReqService: "device",
                    importReqUrl: "/materialsSchedule/importIn",
                    importErrorUrl: ""
                };
                ybWidget.ImportFile(param).then(function (data) {
                    angular.forEach(data, function (item) {
                        item.isChecked = false;
                        pickingBillMaterialChange(item)
                    });
                    $scope.billDetail.detailList = [];
                    $scope.billDetail.detailList = angular.copy(data);
                });
            };

            $scope.importOutMateriel = function () {
                if (!$scope.billDetail.headerInfo.outArea) {
                    ybWidget.ShowToaster('error', '请选择领出库区');
                    return;
                }
                var param = {
                    name: "零件",
                    templateReqService: "device",
                    templateReqUrl: "/materialsSchedule/downloadOut",
                    importReqService: "device",
                    importReqUrl: $scope.isKJ ? '/materialsSchedule/importOut4MaterialFromParts' : '/materialsSchedule/importOut',
                    importErrorUrl: "",
                    otherParams: {
                        warehouseCode: $scope.billDetail.headerInfo.outLocation,
                        areaCode: $scope.billDetail.headerInfo.outArea,
                    }
                };
                ybWidget.ImportFile(param).then(function (data) {
                    $scope.billDetail.detailList = [];
                    $scope.billDetail.detailList = angular.copy(data);
                });
            };

            $scope.delMateriel = function (item, index, e) {
                ybWidget.ShowMessage("确定要删除该记录吗？", MESG_TYPE.Ask).then(function () {
                    $scope.billDetail.detailList.splice(index, 1);
                    EventBus.Publish("setMaterial", item.materialsNo);
                    e.stopPropagation();
                });
            };

            $scope.batchDelMateriel = function () {
                var selectedCount = 0;
                for (var i = 0, len = $scope.billDetail.detailList.length; i < len; i++) {
                    if (!!$scope.billDetail.detailList[i].isChecked) {
                        selectedCount++;
                    }
                }
                if (!selectedCount) {
                    ybWidget.ShowMessage('请选择需要删除的行');
                    return;
                }
                ybWidget.ShowMessage("确定要删除选中的记录吗？", MESG_TYPE.Ask).then(function () {
                    for (var i = $scope.billDetail.detailList.length - 1; i >= 0; i--) {
                        if (!!$scope.billDetail.detailList[i].isChecked) {
                            $scope.billDetail.detailList.splice(i, 1);
                        }
                    }
                    if ($scope.billDetail.detailList.length === 0) {
                        $scope.markInfo.allCheckedMaterial = false;
                    }
                    EventBus.Publish("setMaterial");
                });
            };

            function clearMateriel() {
                $scope.billDetail.detailList = [];
                $scope.markInfo.allCheckedMaterial = false;
                $timeout(function () {
                    if (!$scope.billDetail.headerInfo.outLocation) {
                        EventBus.Publish("clearMaterial");
                    } else {
                        EventBus.Publish("getMaterial");
                    }
                });
            }

            function getDetail(billId ,type) {
                var service = "device";
                if (type == 'del') {
                    var reqUrl = "/materialsSchedule/itemWithDel"; // 已删除详情
                }else {
                    var reqUrl = "/materialsSchedule/item"; // 正常数据详情
                }
                
                var param = {};
                param.id = billId;
                return ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    const resT = resp.value;
                    if (resT && resT.headerInfo && resT.headerInfo.requireTime) {
                        resT.headerInfo.requireTimeShow = $filter("YbDate")(resT.headerInfo.requireTime, "yyyy-MM-dd HH:mm:ss");
                        resT.headerInfo.requireTime = new Date(resT.headerInfo.requireTime);
                    }
                    return resT;
                });
            }

            function viewDetail(item, delType) {
                getDetail(item.id, delType).then(function (data) {
                    if (!data) {
                        return;
                    }

                    $scope.billDetail = data;
                    angular.forEach($scope.billDetail.detailList, function (item) {
                        item.requireTimeShow = $filter("YbDate")(item.requireTime, "yyyy-MM-dd HH:mm:ss")
                        item.postInDateShow = $filter("YbDate")(item.postInDate, "yyyy-MM-dd HH:mm:ss");
                        item.postOutDateShow = $filter("YbDate")(item.postOutDate, "yyyy-MM-dd HH:mm:ss");
                    })
                    if (item.type === 0) {
                        angular.forEach($scope.billDetail.detailList, function (item) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                            item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                            item.inTimeShow = $filter("YbDate")(item.inOutTime, "yyyy-MM-dd HH:mm:ss");
                            item.inTimeShow = $filter("YbDate")(item.inOutTime, "yyyy-MM-dd HH:mm:ss");
                        });
                        $scope.changeTab(3);
                    }
                    if (item.type === 1) {
                        if (!$scope.isSCH) {
                            $scope.billDetail.headerInfo.postInStatusShow = $filter("ProgressStatus")($scope.billDetail.headerInfo.postInStatus, "posting");
                        }
                        $scope.billDetail.headerInfo.postOutStatusShow = $filter("ProgressStatus")($scope.billDetail.headerInfo.postOutStatus, "posting");
                        
                        angular.forEach($scope.billDetail.detailList, function (item) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                            if (!$scope.isSCH) {
                                item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                            }
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                            item.outTimeShow = $filter("YbDate")(item.inOutTime, "yyyy-MM-dd HH:mm:ss");
                        });

                        // 判断是 普通数据详情还是已删除数据详情
                        if (delType) {
                            $scope.changeTab(12);
                        } else {
                            $scope.changeTab(6);
                        }
                    }
                });
            }

            function viewSapError(item) {
                ybWidget.ShowMessage(item.sapErrorMsg, MESG_TYPE.Note);
            }

            $scope.go2adjust = function (item) {
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.billDetail = data;
                    angular.forEach($scope.billDetail.detailList, function (item) {
                        item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                    });
                    $scope.changeTab(7);
                })
            }

            $scope.adjustData = function (valid, item) {
                if (!valid) {
                    $scope.submitted = true;
                    return;
                }
                // 随车表格内需求时间为必填项
                if (!!$scope.isSCH) {
                    let every = $scope.billDetail.detailList.every(item => item.requireTime)
                    if (!every) {
                        return
                    }
                }
                var service = "device";
                var reqUrl = "/materialsSchedule/adjust";
                return ybHttp.Post(service, reqUrl, item).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return false;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    if ($scope.tabFlag.type === 0 || $scope.tabFlag.type === 1) {
                        getDataList(1);
                    } else {
                        getDataList();
                    }
                    $scope.changeTab(0);
                });
            }

            $scope.editData = function (item) {
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.billDetail = data;
                    $scope.tabFlag.type = 2;
                    if (item.type === 0) {
                        angular.forEach($scope.billDetail.detailList, function (item) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                            item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                        });
                        $scope.changeTab(2);
                    }
                    if (item.type === 1) {
                        angular.forEach($scope.billDetail.detailList, function (item) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                            if (!$scope.isSCH) {
                                item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                            }
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                        });
                        $scope.changeTab(5);
                    }
                });
            };

            $scope.printBill = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: item.type === 1 ? "PickingOut" : "PickingIn",
                    recordPrint: {
                        billNo: item.requisitionNo
                    }
                };
                ybWidget.SetIsPrintOrPreview(param).then(function() {
                    getDataList();
                });
            };

            $scope.batchPrint = function () {
                const selectedDatas = $scope.dataList.filter(df => df.isChecked);

                if (selectedDatas.length === 0) {
                    ybWidget.ShowMessage('请选择需要打印的行');
                    return;
                }

                if (new Set(selectedDatas.map(dm => dm.type)).size == 2) {
                    ybWidget.ShowMessage('请选择单据类型相同的行');
                    return;
                }

                var param = {
                    printType: selectedDatas[0].type === 1 ? "PickingOut" : "PickingIn",
                    cusInterface: '/materialsSchedule/itemList',
                    cusParam: { ids: selectedDatas.map(sd => sd.id) },
                    isBatchPrint: true,
                    recordPrint: {
                        billNo: selectedDatas.map(sd => sd.requisitionNo).join(',')
                    }
                };
                ybWidget.SetIsPrintOrPreview(param).then(function() {
                    getDataList();
                });
            };

            $scope.printBillGroupByOutLoc = function (item) {
                var id = item.id;
                var billIds = [];
                billIds.push(id);
                var param = {
                    billId: billIds,
                    printType: item.type === 1 ? "PickingOutGroupByOutLoc" : "PickingInGroupByOutLoc",
                    recordPrint: {
                        billNo: item.requisitionNo
                    }
                };
                ybWidget.SetIsPrintOrPreview(param).then(function() {
                    getDataList();
                });
            };

            $scope.printBill4Details = function (item) {
                var param = {
                    printType: $scope.billDetail.headerInfo.type === 1 ? "PickingOut" : "PickingIn",
                    cusInterface: '/materialsSchedule/itemDetail',
                    cusParam: {
                        id: $scope.billDetail.headerInfo.id,
                        detailIds: [item.id]
                    },
                    recordPrint: {
                        billNo: $scope.billDetail.headerInfo.requisitionNo
                    }
                };
                ybWidget.SetIsPrintOrPreview(param).then(function() {
                    getDataList();
                });
            };

            $scope.batchPrint4Details = function () {
                const selectedIds4Detail = $scope.billDetail.detailList.filter(d => d.isChecked).map(dm => dm.id);
                if (selectedIds4Detail.length === 0) {
                    ybWidget.ShowMessage('请选择需要打印的行');
                    return;
                }

                var param = {
                    printType: $scope.billDetail.headerInfo.type === 1 ? "PickingOut" : "PickingIn",
                    cusInterface: '/materialsSchedule/itemDetail',
                    cusParam: {
                        id: $scope.billDetail.headerInfo.id,
                        detailIds: selectedIds4Detail
                    },
                    recordPrint: {
                        billNo: $scope.billDetail.headerInfo.requisitionNo
                    }
                };
                ybWidget.SetIsPrintOrPreview(param).then(function() {
                    getDataList();
                });
            };

            $scope.checkAmount = function (item, type) {
                var inOrOut = (type === "in") ? "入" : "出";
                item.qtyErr = false;

                if (!item.amount || parseFloat(item.amount) <= 0) {
                    item.qtyErr = true;
                    item.qtyErrMsg = "领料数量不能为空和0";
                    return;
                }
                if (item.amount > 99999.999) {
                    item.amount = 99999.999;
                }
                if (parseFloat(item.amount) < parseFloat(item.realAmount)) {
                    item.qtyErr = true;
                    item.qtyErrMsg = "领料数量不能小于实际" + inOrOut + "库数量";
                }
                if (type === "out") {
                    checkItemStockStatus(item);
                }
            };

            $scope.showQtyTip = function (item) {
                if (!!item.qtyStockErr) {
                    item.showStockErr = true;
                }
            };
            $scope.hideQtyTip = function (item) {
                item.showStockErr = false;
            };

            function checkItemStockStatus(item) {
                UserContextService.SetStockStatusFlag(item, item.amount);

            }

            function checkListValid(type) {
                var inOrOut = (type === "in") ? "入" : "出";
                var isValid = true;
                for (var i = 0, len = $scope.billDetail.detailList.length; i < len; i++) {
                    $scope.billDetail.detailList[i].outErr = false;
                    $scope.billDetail.detailList[i].qtyErr = false;

                    if ($scope.tabFlag.type === 0) {
                        if ($scope.billDetail.detailList[i].outLocation === undefined || $scope.billDetail.detailList[i].outLocation === '') {
                            $scope.billDetail.detailList[i].outErr = true;
                            isValid = false;
                            break;
                        }
                    }

                    if ($scope.billDetail.detailList[i].amount === undefined || !$scope.billDetail.detailList[i].amount || parseFloat($scope.billDetail.detailList[i].amount) <= 0) {
                        $scope.billDetail.detailList[i].qtyErr = true;
                        $scope.billDetail.detailList[i].qtyErrMsg = "领料数量不能为空和0";
                        isValid = false;
                        break;
                    }


                    if ($scope.tabFlag.type === 2) {
                        if (parseInt($scope.billDetail.detailList[i].amount) < parseInt($scope.billDetail.detailList[i].realAmount)) {
                            $scope.billDetail.detailList[i].qtyErr = true;
                            $scope.billDetail.detailList[i].qtyErrMsg = "领料数量不能小于实际" + inOrOut + "库数量";
                            isValid = false;
                            break;
                        }
                    }
                }
                return isValid;
            }

            $scope.saveData = function (isValid, type) {
                
                $scope.submitted = false;
                if (angular.isDefined(isValid) && !isValid) {
                    $scope.submitted = true;
                    return;
                }
                if ($scope.billDetail.detailList.length === 0) {
                    ybWidget.ShowToaster('error', '零件不可为空');
                    return;
                }
                var listValid = checkListValid(type);
                if (!listValid) {
                    $scope.submitted = true;
                    return;
                }
                if (type === "out" && isErrorStockStatusForExcavatorCompany()) {
                    return;
                }
                // 随车表格内需求时间为必填项
                if (!!$scope.isSCH) {
                    let every = $scope.billDetail.detailList.every(item => item.requireTime)
                    if (!every) {
                        return
                    }
                }
                var service = "device";
                var reqUrl = "/materialsSchedule/add";
                if ($scope.tabFlag.type === 2) {
                    reqUrl = "/materialsSchedule/update";
                }
                var param = angular.copy($scope.billDetail);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    if ($scope.tabFlag.type === 0 || $scope.tabFlag.type === 1) {
                        getDataList(1);
                    } else {
                        getDataList();
                    }
                    $scope.changeTab(0);
                });
            };

            function isErrorStockStatusForExcavatorCompany() {
                if (!($scope.isWJ || $scope.isKJ || $scope.isNJ)) {
                    return false;
                }

                for (var i = 0; i < $scope.billDetail.detailList.length; i++) {
                    checkItemStockStatus($scope.billDetail.detailList[i]);
                    if ($scope.billDetail.detailList[i].qtyStockErr && ($scope.billDetail.detailList[i].qtyStockErrLevel > 1)) {
                        ybWidget.ShowMessage("零件" + $scope.billDetail.detailList[i].materialsNo + "，" + $scope.billDetail.detailList[i].materialsDes + "库存不足，单据不能创建，请先删除该零件或取消提交", MESG_TYPE.Error);
                        $scope.clickValue = false;
                        return true;
                    }
                }
                return false;
            }

            $scope.obligate = function (item) {
                item.obligateDisabled = true;
                var service = "device";
                var reqUrl = "/warehouseInventory/createObligateOrder";
                var param = {};
                param.id = item.id;
                param.businessOrder = item.requisitionNo;
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        item.obligateDisabled = false;
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                });
            };

            $scope.handleRevoke = function (item) {
                ybWidget.ShowMessage("确定要撤回吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = "/materialsSchedule/revoke?";
                    var param = {
                        billNo: $scope.billDetail.headerInfo.requisitionNo,
                        materialNo: item.materialsNo
                    };
                    reqUrl += new URLSearchParams(param).toString()
                    ybHttp.Post(service, reqUrl).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        item.revokeDisabled = true;
                        ybWidget.ShowToaster('success', '操作成功');
                    });
                }, function () {});
            };

            $scope.posting = function (item, inOut, materialsNo) {
                ybWidget.ShowMessage("确定要过账吗？", MESG_TYPE.Ask).then(function () {
                    var postingType = inOut === 1 ? "Out" : "In";
                    item["posting" + postingType + "Disabled"] = true;
                    var service = "device";
                    var reqUrl = "/warehouseInventory/obligatePosting";
                    var param = {};
                    param.businessOrder = item.requisitionNo;
                    param.materialsNo = materialsNo;
                    param.inOut = inOut;
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            item["posting" + postingType + "Disabled"] = false;
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        if (!!item.id) {
                            getDetail(item.id).then(function (data) {
                                if (!data) {
                                    return;
                                }

                                $scope.billDetail = data;
                                if ($scope.billDetail.headerInfo.type === 0) {
                                    angular.forEach($scope.billDetail.detailList, function (item) {
                                        item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                                    });
                                }
                                if ($scope.billDetail.headerInfo.type === 1) {
                                    angular.forEach($scope.billDetail.detailList, function (item) {
                                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                                    });
                                }
                            });
                        }
                        getDataList();
                    });
                }, function () {});
            };

            $scope.mandatoryFinish = function (type,billNo, billId, materialsNo) {
                ybWidget.ShowMessage("确定要结单吗？", MESG_TYPE.Ask).then(function () {
                    var service = "device";
                    var reqUrl = type === 1 ? "/materialsSchedule/manualFinishOut" :"/materialsSchedule/manualFinishIn";
                    var param = {
                        billNo: billNo,
                        materialsNo: materialsNo ? materialsNo : null
                    };
                    ybHttp.Post(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        if (!!billId) {
                            getDetail(billId).then(function (data) {
                                if (!data) {
                                    return;
                                }

                                $scope.billDetail = data;
                                if ($scope.billDetail.headerInfo.type === 0) {
                                    angular.forEach($scope.billDetail.detailList, function (item) {
                                        item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                                    });
                                }
                                if ($scope.billDetail.headerInfo.type === 1) {
                                    angular.forEach($scope.billDetail.detailList, function (item) {
                                        item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                                    });
                                }
                            });
                        }
                        getDataList();
                    });
                }, function () {});
            };

            $scope.cancel = function () {
                $scope.changeTab(0);
            };

            $scope.change2UpperCase = function (item) {
                if (item.outLocation) {
                    item.outLocation = item.outLocation.toUpperCase()
                }
            };

            $scope.selDeliveryNotes = function() {
                ybWidget.DataSelector({
                    title: '选择发货通知单',
                    url: '/materialsSchedule/selectDeliveryNotes',
                    method: 'Get',
                    isNotFilterParam: true,
                    keyWords: [{label: '发货通知单号', field: 'deliveryNoteNo'}],
                    fields: [
                        {
                            name: '发货通知单号',
                            dataField: 'deliveryNo',
                            width: "50%"
                        },
                        {
                            name: '客户名称',
                            dataField: 'purchaseOrgName',
                            width: "50%"
                        }
                    ]
                }).then(function (data) {
                    ybHttp.Get('device', '/materialsSchedule/deliveryNoteDetails', { deliveryNoteId: data.id }).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        if (!resp.value) return;
                        angular.forEach(resp.value, function (item) {
                            item.isChecked = false;
                            pickingBillMaterialChange(item);
                        });
                        $scope.billDetail.detailList = resp.value;
                    });
                })
            };

            $scope.auditBill = function (item) {
                $scope.auditMessage = {};
                $scope.auditId = item.id;
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.billDetail = data;
                    if (item.type === 0) {
                        angular.forEach($scope.billDetail.detailList, function (item) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "inDepot");
                            item.postInStatusShow = $filter("ProgressStatus")(item.postInStatus, "posting");
                        });
                        $scope.changeTab(8);
                    }
                    if (item.type === 1) {
                        angular.forEach($scope.billDetail.detailList, function (item) {
                            item.statusShow = $filter("ProgressStatus")(item.status, "outDepot");
                            item.postOutStatusShow = $filter("ProgressStatus")(item.postOutStatus, "posting");
                        });
                        $scope.changeTab(9);
                    }
                });
            };

            $scope.auditMessage = {};
            $scope.handleAudit = function(type, ids, msg) {
                var service = "device";
                var reqUrl = "";
                var param = {
                    ids: ids || [$scope.auditId]
                };

                var msgStr = msg || $scope.auditMessage.value;
                if (type == 1) {
                    // 通过
                    reqUrl = "/materialsSchedule/commonAuditPass"
                } else if (type == 2) {
                    // 拒绝
                    if (!msgStr) {
                        ybWidget.ShowMessage("请填写审核意见", MESG_TYPE.Error);
                        return
                    }
                    reqUrl = "/materialsSchedule/commonAuditReject"
                }
                param.auditRemark = msgStr;

                return ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    getDataList();
                    $scope.changeTab(0);
                });
            };

            $scope.auditBillBatch = function () {
                $scope.auditMessage = {};
                var ids = [];
                for (var i = 0, len = $scope.dataList.length; i < len; i++) {
                    if (!!$scope.dataList[i].isChecked) {
                        ids.push($scope.dataList[i].id);
                    }
                }
                if (ids.length === 0) {
                    ybWidget.ShowMessage('请正确选择需要审核的行');
                    return;
                }
                ybWidget.AuditMsg({
                    passCb: function(val) {
                        return $scope.handleAudit(1, ids, val)
                    },
                    refuseCb: function(val) {
                        return $scope.handleAudit(2, ids, val)
                    }
                })
            };

            $scope.go2inbound = function (item) {
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.billDetail = data;
                    $scope.changeTab(10);
                });
            };
            $scope.go2outbound = function (item) {
                getDetail(item.id).then(function (data) {
                    if (!data) {
                        return;
                    }
                    $scope.billDetail = data;
                    $scope.changeTab(13);
                });
            };
            $scope.selDepot = function (item) {
                var param = {
                    warehouseCode: $scope.billDetail.headerInfo.inLocation
                };
                ybWidget.DepotSelector(param).then(function (data) {
                    item.stockLocation = data.locationCode;
                })
            };
            $scope.outDepot = function (item) {
                var param = {
                    warehouseCode: $scope.billDetail.headerInfo.outLocation
                };
                ybWidget.DepotSelector(param).then(function (data) {
                    item.stockLocation = data.locationCode;
                })
            };
            $scope.clearDepot = function (item) {
                item.stockLocation = "";
            };

            $scope.inboundSubmit = function () {
                var service = "device";
                var reqUrl = "/warehouseInboundRecord/receiveLl";
                var param = {
                    headerInfo: {
                        businessOrder: $scope.billDetail.headerInfo.requisitionNo
                    },
                    detailList: []
                };
                
                var errorCount = 0;
                angular.forEach($scope.billDetail.detailList, function (item) {
                    item.locationErr = false;
                    if (!item.stockLocation) {
                        item.locationErr = true;
                        errorCount++;
                    } else {
                        const { materialsNo, warehouseCode, areaCode, stockLocation } = item;
                        param.detailList.push({ materialNo: materialsNo, warehouseCode, areaCode, locationCode: stockLocation });
                    }
                });
                if (errorCount > 0) {
                    return;
                }

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };
            $scope.outboundSubmit = function () {
                var service = "device";
                var reqUrl = "/warehouseDeliveryRecord/receive";
                var param = {
                    headerInfo: {
                        businessOrder: $scope.billDetail.headerInfo.requisitionNo
                    },
                    detailList: []
                };
                
                var errorCount = 0;
                angular.forEach($scope.billDetail.detailList, function (item) {
                    item.locationErr = false;
                    if (!item.stockLocation) {
                        item.locationErr = true;
                        errorCount++;
                    } else {
                        const { materialsNo, warehouseCode, areaCode, stockLocation } = item;
                        param.detailList.push({ materialNo: materialsNo, warehouseCode, areaCode, locationCode: stockLocation });
                    }
                });
                if (errorCount > 0) {
                    return;
                }

                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    getDataList();
                });
            };
            $scope.batchPosting = function() {
                const conditionList = $scope.dataList.filter(df => df.isChecked).map(dm => {
                    return {
                        businessOrder: dm.requisitionNo,
                        inOut: 1,
                    }
                })

                if (conditionList.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }

                ybWidget.ShowMessage("确定要批量过账吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post('device', '/warehouseInventory/obligatePostingBatch', conditionList).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            $scope.batchReserve = function() {
                const conditionList = $scope.dataList.filter(df => df.isChecked).map(dm => {
                    return {
                        businessOrder: dm.requisitionNo,
                        id: dm.id,
                    }
                })

                if (conditionList.length === 0) {
                    ybWidget.ShowMessage('请选择需要操作的行');
                    return;
                }

                ybWidget.ShowMessage("确定要批量创建预留吗？", MESG_TYPE.Ask).then(function () {
                    ybHttp.Post('device', '/warehouseInventory/createObligateOrderBatch', conditionList).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        ybWidget.ShowToaster('success', '操作成功');
                        getDataList();
                    });
                });
            };

            (function () {
                // $scope.condition.createName = $scope.isCHY ? $scope.curUser.name : undefined; // 后期可能会要
                $scope.condition.createPartName = $scope.isJCH ? $scope.curUser.deptName : undefined;
                getDataList();
                $scope.setFixedWidth();
                UserContextService.SetBtnPermission(setBtnPermission);
                ChyTable()
            })();
        }]);
})(window, window.angular);
